var util = require('../../utils/util.js')
Page({
  data: {
    name: '',
    place: '',
    phone: '',
    symptom: '',
    imgList: []
  },

  onLoad: function (options) {
    var phone = wx.getStorageSync('phone');
    this.setData({ phone: phone })
  },

  inputname: function (event) {
    this.setData({
      name: event.detail.value
    })
  },

  inputplace: function (event) {
    this.setData({
      place: event.detail.value
    })
  },

  inputdemand: function (event) {
    this.setData({
      symptom: event.detail.value
    })
  },

  ChooseImage() {
    wx.chooseImage({
      count: 3, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'],
      success: (res) => {
        wx.showLoading({
          title: '图片上传中...',
        })
        var time = util.formatTimestring(new Date());
        var FilePaths = []
        const temFilePaths = res.tempFilePaths
        let promiseArr = [];
        for (let i = 0; i < temFilePaths.length; i++) {
          let promise = new Promise((resolve, reject) => {
            var randstring = Math.floor(Math.random() * 1000000).toString() + '.png'
            randstring = time + '-' + randstring
            wx.cloud.uploadFile({
              cloudPath: 'orderimg/' + randstring,
              filePath: temFilePaths[i], // 文件路径
              success: res => {
                // get resource ID
                console.log(res.fileID)
                FilePaths[i] = res.fileID
                resolve(res);
              },
              fail: err => {
                reject(error);
              }
            })
          })
          promiseArr.push(promise)
        }
        Promise.all(promiseArr).then((result) => {
          if (this.data.imgList.length != 0) {
            this.setData({
              imgList: this.data.imgList.concat(FilePaths)
            })
          } else {
            this.setData({
              imgList: FilePaths
            })
          }
          wx.hideLoading()
        })
      }
    });
  },

  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },

  DelImg: function (event) {
    console.log(event.currentTarget.dataset.id)
    var id = event.currentTarget.dataset.id
    var imgList = this.data.imgList
    wx.cloud.deleteFile({
      fileList: [imgList[id]]
    }).then(res => {
      imgList.splice(id, 1)
      this.setData({ imgList: imgList })
      console.log(res.fileList)
    }).catch(error => {
    })
  },

  check() {
    var name = this.data.name
    var place = this.data.place
    var symptom = this.data.symptom
    if (name == '' || place == '' || symptom == '') {
      return false
    }
    else {
      return true
    }
  },

  submitform: function (event) {
    var userInfo = wx.getStorageSync('userInfo');
    if (this.check()) {
      wx.cloud.callFunction({
        name: 'orderform',
        data: {
          $url: "add",
          name: this.data.name,
          phone: this.data.phone,
          place: this.data.place,
          symptom: this.data.symptom,
          url: this.data.imgList,
          avatarUrl: userInfo.avatarUrl
        },
        success: res => {
          wx.showToast({
            title: '保修单提交成功',
            icon: 'success',
            duration: 2000
          })
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }
    else {
      wx.showToast({
        title: '信息填写不全！',
        icon: 'success',
        duration: 2000
      })
    }
  },
})